package mya.mes.mdc.logic.status.mqtt;

import mya.common.jpa.context.NoLoginLogic;
import mya.common.jpa.context.ServiceContext;
import mya.common.util.Utils;
import mya.mes.mdc.local.status.MdcStatusResult;
import mya.mes.mdc.local.status.StatusService;
import mya.mes.mdc.logic.status.build.StatusServiceImpl;
import mya.mes.mdc.share.plc.PlcTagVoProcessor;
import mya.mes.mdc.share.vo.plc.PlcTagVo;

import java.util.List;

public class MqttStatusProcessor implements PlcTagVoProcessor<MdcStatusResult> {

    final Long zoneId;

    private final StatusService statusService;

    public MqttStatusProcessor(final Long zoneId) {
        this.zoneId = zoneId;
        this.statusService = new StatusServiceImpl(zoneId);
    }

    @Override
    public String getId() {
        return "MQTT_STATUS_PROCESSOR_DEFAULT";
    }

    @Override
    public MdcStatusResult process(Long mqttMsgId, List<PlcTagVo> statusVoList) throws Exception {

        if (Utils.isNullOrEmpty(statusVoList)) {
            return null;
        }

        MdcStatusResult alarmResult = statusProcess(statusVoList);


        return alarmResult;
    }


    private MdcStatusResult statusProcess(List<PlcTagVo> voList) throws Exception {

        NoLoginLogic logic = new NoLoginLogic("0") {
            @Override
            protected Object process(ServiceContext context) throws Exception {

                return statusService.processStatus(context, voList);

            }
        };

        try {
            return (MdcStatusResult) logic.callLogic();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

}
